package com.banzhi.shop.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.banzhi.shop.R;
import com.banzhi.shop.adapter.CommonAdapter;
import com.banzhi.shop.entity.videocollege.VideoCollegeCallBack;
import com.banzhi.shop.entity.videocollege.VideoCollegeImpl;
import com.banzhi.shop.entity.videocollege.VideoOrder;
import com.banzhi.shop.entity.videocollege.VideoOrder_Data;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.EndlessRecyclerOnScrollListener;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.ShareUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.squareup.picasso.Picasso;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.banzhi.shop.fragment.GoodsParameterFrg.TAG;

/**
 * Created by Administrator on 2018/5/10.
 */

public class VideoOrderFragment extends android.support.v4.app.Fragment implements VideoCollegeCallBack, SwipeRefreshLayout.OnRefreshListener {

    private SwipeRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private VideoCollegeImpl model;
    private int page = 1;
    private List<VideoOrder_Data> videoOrderData = new ArrayList<VideoOrder_Data>();
     private CommonAdapter<VideoOrder_Data> adapter;
    private LinearLayoutManager LayoutManager;
    private RelativeLayout rlnodata;
    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_video_order, container, false);
        setinits(view);
        setlisteners();
        model = new VideoCollegeImpl();
        LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
        model.getData(page, "2", this);
        return view;
    }

    private void setinits(View view) {
        refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.srl_listview);
        refreshLayout.setColorSchemeResources(R.color.themeColor);
        refreshLayout.setProgressBackgroundColorSchemeResource(R.color.colorWhite);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        LayoutManager = new LinearLayoutManager(getActivity());
        LayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(LayoutManager);
        rlnodata = (RelativeLayout) view.findViewById(R.id.rl_nodata);

    }

    private void setlisteners() {
        refreshLayout.setOnRefreshListener(this);
        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(LayoutManager) {
            @Override
            public void onLoadMore() {
                page++;
                LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
                model.getData(page, "2", VideoOrderFragment.this);
            }
        });

    }

    @Override
    public void onRefresh() {
        LoadingDialogUtils.showProgress(getActivity(), "正在加载,请稍后~");
        page = 1;
        model.getData(page, "2", this);

    }

    /*
    *
    *   获取数据成功
    * */
    @Override
    public void ongetDataSuccess(Object object) {

     //   LogUtils.d("hththt",object.toString());
       final List<VideoOrder_Data> data = (List<VideoOrder_Data>) object;
        if (page == 1) {
            videoOrderData.clear();
        } else {if (data.size() == 0) {
               return;
            }
        }
       if (data.size() > 0) {
            videoOrderData.addAll(data);
        }

       if (videoOrderData.size() == 0) {
            rlnodata.setVisibility(View.VISIBLE);
        } else {
            rlnodata.setVisibility(View.GONE);
        }
        //Log.d(TAG, "ongetDataSuccess: "+data.size());
        if (adapter == null) {adapter = new CommonAdapter<VideoOrder_Data>(getActivity(), R.layout.item_video_order, videoOrderData) {
            @Override
            public void convert(ViewHolder holder, final VideoOrder_Data data, int position) {
                holder.setText(R.id.tv_ordernum, data.getOrderSn());
                holder.getTextView(R.id.tv_fuzhi).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ShareUtils.copyLianjie(getActivity(), data.getOrderSn());}
                });
                holder.setText(R.id.tv_video_name, data.getGoodsName());
                if (ASUtils.isCanUse(data.getCover())) {
                    Picasso.with(getContext()).load(data.getCover()).placeholder(R.mipmap.zhanweitu2).into(holder.getImageView(R.id.iv_image));
                }
                holder.setText(R.id.tv_video_yuan, data.getPrice());

                holder.setText(R.id.tv_teacher, data.getTeacherName());
                holder.setText(R.id.tv_statu_time, data.getPayTime());

            }
        };
            recyclerView.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
 }




    @Override
    public void ongetDataError(Object object) {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(getActivity(), jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));

    }
}
